//
//  SubjectTableViewController.swift
//  SCUTWiki
//
//  Created by mac on 2020/12/15.
//  Copyright © 2020 wwszhw. All rights reserved.
//

import UIKit

class SubjectTableViewController: UITableViewController, StarDelegate {

    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var subjectLabel: UILabel!
    @IBOutlet weak var introLabel: UILabel!
    var subject_name:String?
    var type_id:Int?
    var intro:String?
    
    var sqlManager = SQLiteManager.shareInstance
    var articlesArray = [ArticleModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        iconImageView.image = UIImage(named: subject_name!)
        subjectLabel.text = subject_name
        introLabel.text = intro
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(false)
        if self.tableView.indexPathForSelectedRow == nil{//为nil说明是从别的页面跳转来的而不是从文章页面回来的
            loadDataOfaSubject()
            self.tableView.reloadData()
        }
    }
    
    // MARK: - Table view data source
    func loadDataOfaSubject()->Void{
        self.articlesArray.removeAll()
        if sqlManager.openDB(){
            if let articles = sqlManager.execQuerySQL(sql: "select * from articles where type_id=\(type_id ?? 0)"){
                for (_,row) in articles.enumerated(){
                    articlesArray.append(ArticleModel(rowData: row))
                }
            }else{
                articlesArray.append(ArticleModel(id:-1, title: "暂无该内容的无数据", content: "暂无该内容的无数据", type_id: -1, collected: false))
            }
        }
        sqlManager.closeDB()
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return articlesArray.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "subjectTableViewCell", for: indexPath)

        // Configure the cell...
        cell.textLabel?.text = articlesArray[indexPath.row].title

        return cell
    }
    
    // MARK: - Navigation
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let dest_view_controller = segue.destination as? ArticleViewController{
            if let select_index = self.tableView.indexPathForSelectedRow?.row{
                dest_view_controller.articleObject = self.articlesArray[select_index]
                dest_view_controller.starDelegate = self
            }
        }
    }
    
    func changeCollection(collected: Bool) {
        if let select_index = self.tableView.indexPathForSelectedRow{
            if sqlManager.openDB(){
                if sqlManager.execUpdateSQL(sql: "update articles set collected=\(collected) where id=\(articlesArray[select_index.row].id)"){
                    print("subject tableview 更改db收藏")
                }
            }
            sqlManager.closeDB()
            articlesArray[select_index.row].collected = collected
        }
    }
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    

}
